from django.http import JsonResponse
from .res_code import Code
from apps.users.models import *


def to_json_data(errno=Code.OK, errmsg='', data=None, **kwargs):
    json_dict = {'errno': errno, 'errmsg': errmsg, 'data': data}

    if kwargs:
        json_dict.update(kwargs)

    return JsonResponse(json_dict)


def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        user = request.user
        if user.is_superuser:
            print('这是定义装饰器')
            print('判断用户是否登录，是否有相关权限')
            return func(request, *args, **kwargs)
        else:
            return to_json_data(errno=Code.ROLEERR, errmsg="用户权限不够")
    return wrapper